package com.sinya.shoppingmall.ui.recomend;

import android.content.Context;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.loopj.android.image.SmartImageView;
import com.sinya.shoppingmall.R;
import com.sinya.shoppingmall.bean.Constants;
import com.sinya.shoppingmall.bean.ProductBean;
import com.sinya.shoppingmall.bean.RecommendBean;
import com.sinya.shoppingmall.widget.MeasureGridView;
import com.sinya.shoppingmall.widget.MyImageView;
import com.sinya.shoppingmall.widget.MyListView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author KoizumiSinya
 * @date 2016/1/4.
 */
public class RecomendListView extends LinearLayout {

    private Context context;

    private SlidingPaneLayout mSlidingPaneLayout;

    /**
     * 根据传入的id 判断RecomendListView控件列表需要显示什么内容
     */
    private int id;

    /**
     * 数据实体类
     */
    private RecommendBean recommendBean;

    /**
     * 存放数据信息集合（从recommendBean中获取）
     */
    private List<ProductBean> value;

    /**
     * 用于装载 孕妈专区的模拟数据界面的外层布局
     */
    private LinearLayout ll_picture;

    /**
     * 装填商品logo的adapter
     */
    private LogoGridViewAdapter adapter;

    // [+] 模拟picture2数据

    private MyListView mListView;
    private PictureMoveViewAdapter mAdapter;

   /* private int type = 1;
    private int mCurPosition;
    private int lastPosition;
    private boolean isVisible = false;*/

    /**
     * 存放需要切换的Item子项
     */
    private Map<Integer, TempPictureView> tempViewMap;

    // [-] 模拟picture2数据
    // [+] 构造函数

    public RecomendListView(Context context) {
        super(context);
        this.context = context;
    }

    public RecomendListView(Context context, SlidingPaneLayout mSlidingPaneLayout, int id, RecommendBean bean) {
        this(context);
        this.id = id;
        this.mSlidingPaneLayout = mSlidingPaneLayout;
        this.recommendBean = bean;

        initView();
    }

    // [-] 构造函数
    // [+] Init

    private void initView() {
        if (id != 2) {
            this.addView(View.inflate(context, R.layout.view_recommend_list, null));
            ll_picture = (LinearLayout) findViewById(R.id.ll_picture);
        }

        getData();

        //此处是模拟数据 均为本地设置
        switch (id) {
            case 1:
                setPictures1();
                break;
            case 2:
                this.addView(View.inflate(context, R.layout.view_recommend_picture2, null));
                mListView = (MyListView) findViewById(R.id.pic2_listview);

                tempViewMap = new HashMap<>();

                setPictures2();
                break;
            case 3:
                setPictures3();
                break;
            case 4:
                setPictures4();
                break;
            case 5:
                setPictures5();
                break;
            case 6:
                setPictures6();
                break;
            case 7:
                setPictures7();
                break;
            case 8:
                setPictures8();
                break;
            case 9:
                setPictures9();
                break;
            case 10:
                setPictures10();
                break;

            default:
                break;
        }

        //根据value提取图片资源 动态设置GridView
        if (id != 2) {
            setGridView();
        }

    }

    public void getData() {
        if (recommendBean.value != null) {
            this.value = recommendBean.value;
        }
    }

    private void setPictures1() {
        ll_picture.addView(View.inflate(context, R.layout.view_recommend_picture1, null), 0);
        ImageView image1 = (ImageView) findViewById(R.id.img_top);
        image1.setImageResource(R.mipmap.recommend_id1_top);
        ImageView image2 = (ImageView) findViewById(R.id.img_center1_left);
        image2.setImageResource(R.mipmap.recommend_id1_center1);
        ImageView image3 = (ImageView) findViewById(R.id.img_center1_right);
        image3.setImageResource(R.mipmap.recommend_id1_center2);
        ImageView image4 = (ImageView) findViewById(R.id.img_center2_left);
        image4.setImageResource(R.mipmap.recommend_id1_center3);
        ImageView image5 = (ImageView) findViewById(R.id.img_center2_right);
        image5.setImageResource(R.mipmap.recommend_id1_center4);
        ImageView image6 = (ImageView) findViewById(R.id.img_buttom);
        image6.setImageResource(R.mipmap.recommend_id1_buttom);
    }

    private void setPictures2() {
        mAdapter = new PictureMoveViewAdapter();
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                replaceImageShow(tempViewMap.get(position));
                for (Integer i : tempViewMap.keySet()) {
                    if (i != position) {
                        replaceViewCotent(tempViewMap.get(i));
                    }
                }


            }
        });
    }

    private void setPictures3() {

    }

    private void setPictures4() {

    }

    private void setPictures5() {

    }

    private void setPictures6() {

    }

    private void setPictures7() {

    }

    private void setPictures8() {

    }

    private void setPictures9() {

    }

    private void setPictures10() {

    }

    private void setGridView() {
        MeasureGridView gridView = (MeasureGridView) findViewById(R.id.gridview);
        adapter = new LogoGridViewAdapter();
        gridView.setAdapter(adapter);
    }

    // [-] Init
    // [+] click

    /**
     * 避免与SlidingPaneLayout滑动重叠
     *
     * @param
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int act = ev.getAction();
        if (act == MotionEvent.ACTION_DOWN) {
            // 处理侧滑冲突的问题
            if (mSlidingPaneLayout != null) {
                mSlidingPaneLayout.requestDisallowInterceptTouchEvent(true);
            }

        } else if (act == MotionEvent.ACTION_UP) {
            // 处理侧滑冲突的问题
            if (mSlidingPaneLayout != null) {
                mSlidingPaneLayout.requestDisallowInterceptTouchEvent(false);
            }
        }

        return super.onInterceptTouchEvent(ev);
    }

    // [-] click
    // [+] Adapter

    /**
     * 通用的gridViewAdapter，针对每个页面的[其他买家喜欢的品牌]内容
     */
    private class LogoGridViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (value != null && value.size() > 0) {
                return value.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return value.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(context, R.layout.item_recommend_gridview, null);
            SmartImageView img = (SmartImageView) convertView.findViewById(R.id.img_smart);
            img.setImageUrl(Constants.BASE_URL + "/" + value.get(position).pic);
            return convertView;
        }
    }

    /**
     * 针对图片滑动效果的页面的adapter
     */
    private final class PictureMoveViewAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 20;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.item_recommend_picture2, null);
                holder = new Holder();
                holder.imageView = (MyImageView) convertView.findViewById(R.id.imageview);
                holder.ll_type = (LinearLayout) convertView.findViewById(R.id.ll_type);

                tempViewMap.put(position, new TempPictureView(holder.imageView, holder.ll_type));
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
                holder.imageView.reSet();   //在ListView重用时，进行一次复位操作。
                holder.imageView.setVisibility(VISIBLE);
                holder.ll_type.setVisibility(GONE);

            }
            return convertView;
        }

        private class Holder {
            public MyImageView imageView;
            public LinearLayout ll_type;

        }
    }

    // [-] Adapter
    // [+] Class

    public class TempPictureView {
        public MyImageView image;
        public LinearLayout ll_content;

        public TempPictureView(MyImageView image, LinearLayout ll_content) {
            this.image = image;
            this.ll_content = ll_content;
        }

    }

    /**
     * 关闭Item位置的图片
     */
    private void replaceViewCotent(TempPictureView v) {
        if (v != null) {
            v.image.setVisibility(View.VISIBLE);
            v.ll_content.setVisibility(View.GONE);
        }
    }

    /**
     * 根据状态切换
     */
    private void replaceImageShow(TempPictureView v) {
        if (v != null) {
            if (v.image.getVisibility() == View.VISIBLE) {
                v.image.setVisibility(View.GONE);
            } else {
                v.image.setVisibility(View.VISIBLE);
            }

            if (v.ll_content.getVisibility() == View.VISIBLE) {
                v.ll_content.setVisibility(View.GONE);
            } else {
                v.ll_content.setVisibility(View.VISIBLE);
            }
        }
    }

    // [-] Class
}
